package org.medicine.action;

import com.opensymphony.xwork2.ModelDriven;
import org.apache.log4j.Logger;
import org.medicine.model.Person;
import org.medicine.service.PersonService;
import org.medicine.vo.PersonVO;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;


@Component("login")
@Scope("prototype")
public class LoginAction extends MyActionSupport implements ModelDriven<PersonVO> {

    private PersonVO personVO = new PersonVO();
    private final static Logger logger = Logger.getLogger(LoginAction.class);
    private PersonService personService;

    @Override
    public String execute() throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userName", personVO.getUserName());
        Person user = (Person) personService.findSpecialObject(Person.class, params);
        if (user == null) {
            personVO.setResultMessage("登陆失败！");
            return ERROR;
        }

        // 验证密码正确与否
        if (personVO.getPassword().equals(user.getPassword())) {
            logger.info(user.getId() + " login the system by security way");
            view = "/page/Medicine/Medicinelist.jsp";
            return VIEW;
        }
        personVO.setResultMessage("密码错误！");
        return ERROR;
    }

    @Override
    public PersonVO getModel() {
        return personVO;
    }

    @Resource
    public void setPersonService(PersonService personService) {
        this.personService = personService;
    }
}
